import { Injectable, inject } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';

@Injectable({
  providedIn: 'root'
})
export class MessageService {
  private nzMessage = inject(NzMessageService);

  /**
   * 显示成功消息
   */
  success(message: string, duration: number = 3000): void {
    this.nzMessage.success(message, { nzDuration: duration });
  }

  /**
   * 显示错误消息
   */
  error(message: string, duration: number = 5000): void {
    this.nzMessage.error(message, { nzDuration: duration });
  }

  /**
   * 显示警告消息
   */
  warning(message: string, duration: number = 4000): void {
    this.nzMessage.warning(message, { nzDuration: duration });
  }

  /**
   * 显示信息消息
   */
  info(message: string, duration: number = 3000): void {
    this.nzMessage.info(message, { nzDuration: duration });
  }

  /**
   * 显示加载消息
   */
  loading(message: string = '加载中...', duration: number = 0): string {
    return this.nzMessage.loading(message, { nzDuration: duration }).messageId;
  }

  /**
   * 移除指定消息
   */
  remove(messageId?: string): void {
    this.nzMessage.remove(messageId);
  }

  /**
   * 清除所有消息
   */
  clear(): void {
    this.nzMessage.remove();
  }

  /**
   * 处理HTTP错误响应
   */
  handleHttpError(error: any): void {
    const errorMessage = error?.error?.userMessage || error?.message || '发生未知错误';
    this.error(errorMessage);
  }

  /**
   * 处理成功响应
   */
  handleSuccess(message: string = '操作成功'): void {
    this.success(message);
  }
} 